package com.example.test2.controller.api;

import com.example.test2.bean.Person;
import com.example.test2.bean.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class ParamController {

    private static final Logger LOG = LoggerFactory.getLogger(ParamController.class);


    /**
     * get 获得参数
     * @param sn
     * @return
     */
    @RequestMapping(value = "/param/path/{sn}")
    public String getParam(@PathVariable String sn){
        return "接受参数:"+sn;
    }

    /**
     * post get 都可以
     * @param username
     * @return
     */
    @RequestMapping(value = "/param/req")
    public String reqParam(String username){
        return "接收到参数:"+username;
    }

    /**
     * get post 都可以
     * @param person
     * @return
     */
    @RequestMapping(value = "/param/model")
    public String reqParam(Person person){
        return "接收到参数:"+person.getAge()+" "+person.getSex();
    }

    /**
     * 这个传过来必须是json raw格式
     * @param person
     * @return
     */
    @RequestMapping(value = "/param/json")
    public Person jsonParam(@RequestBody Person person){
        LOG.info("==========print log  ssssss  ==========");
        return person;
    }

    /**
     * 这个传过来必须是json raw格式
     * @param person 返回值进一步封装
     * @return
     */
    @RequestMapping(value = "/param/jsonResult")
    public Result<Person> jsonParamResultCode(@RequestBody Person person){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return Result.success(person);
    }

}
